package com.manolo.framework.connection.client;

import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashMap;

import com.google.gson.Gson;
import com.manolo.framework.connection.entity.Date;
import com.manolo.framework.connection.entity.Message;
import com.manolo.framework.connection.entity.Player;
import com.manolo.framework.listener.DateListener;
import com.manolo.framework.listener.event.DateEvent;

public class ClientPlayerSender implements Runnable, DateListener {
	private PrintWriter out;
	private DateEvent event = null;
	private Player player;

	public ClientPlayerSender(OutputStream outPutStream, Player player) {
		this.player = player;
		this.out = new PrintWriter(outPutStream, true);
	}

	public void send() {
		// System.out.println(playerId);
		Gson gson = new Gson();
		String messagem = gson.toJson(player);
		// System.out.println(playerId+"::::"+json);
		out.println(messagem);
	}

	@Override
	public void run() {
		while (true) {

			try {
				synchronized (this.player) {
					this.player.wait();
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			send();
		}
	}

	@Override
	public void moveAction(DateEvent event) {
		this.event = event;
		synchronized (this.player) {

			this.player.getMessages().clear();
			player.getMessages().addAll(event.getMessages());
			this.player.notifyAll();
		}
	}
}
